<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">组织审核</span>
  </div>
  <!-- 组织搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="申请人">
        <el-input placeholder="请输入申请人" v-model="auditSearchData.username"></el-input>
      </el-form-item>
      <el-form-item label="角色类型" style="width: 170px">
        <el-select placeholder="请选择" v-model="auditSearchData.role" @change="search">
          <el-option :label="item.label" :value="item.value" v-for="item in auditOptions"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="research">重置</el-button>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 组织申请表格 -->
  <el-card style="margin: 20px">
    <el-radio-group style="margin-bottom: 15px;" v-model="status" @change="search">
      <el-radio-button value="20" size="large">已审核</el-radio-button>
      <el-radio-button value="10" size="large">待审核</el-radio-button>
      <el-radio-button value="30" size="large">已拒绝</el-radio-button>
    </el-radio-group>
    <el-table :data="auditDataOrg">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="username" link label="申请人"></el-table-column>
      <el-table-column align="center" prop="mobile" label="联系方式"></el-table-column>
      <el-table-column align="center" prop="role" label="角色类型" :formatter="roleTypeFormatters"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link v-if="status==10" @click="auditPass(scope.row.id)">通过</el-button>
          <el-button type="primary" size="small" link v-if="status==10" @click="auditReject(scope.row.id)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//获取当前登录的用户
const user = ref(getUser());
//定义一个全局的用户Id
let userId = user.value.id;
//定义审核的类型
const status = ref(10);
//定义查询对象
const auditSearchData = ref({
  username:'',
  status:'',
  role:'',
  orgId:''
});
//定义数组存储查询orgId的数据
const auditData = ref([]);
//定义数组存储审核的数据
const auditDataOrg = ref([]);
//定义函数重置搜索内容
const research = () => {
  auditSearchData.value = {};
  search();
}
//定义一个数组用于存储查询角色类型
const auditOptions = ref([
  {label:'行政',value:'10'},
  {label:'财务',value:'20'},
  {label:'法务',value:'30'},
  {label:'人事',value:'40'},
  {label:'IT',value:'50'},
  {label:'其他',value:'60'}
])
//定义函数查看当前用户创建的组织id,再通过用户id定义函数搜索查询的内容
const search = () => {
  let level = '30';
  let data = qs.stringify({userId:userId,level:level});
  axios.get(BASE_URL+"/isms/role/select?"+data)
      .then((response) => {
        if (response.data.code == 200){
          auditData.value = response.data.data[0];
          // console.log(auditData.value);
          //通过组织id获取到查询的内容
          if (auditData.value != null){
            auditSearchData.value.orgId = auditData.value.orgId;
            auditSearchData.value.status = status.value;
            let data = qs.stringify(auditSearchData.value);
            axios.get(BASE_URL+"/isms/role/select?"+data)
                .then((response) => {
                  if (response.data.code == 200){
                    // ElMessage.success("查询成功!");
                    auditDataOrg.value = response.data.data;
                    // console.log(auditDataOrg.value);
                  }else {
                    ElMessage.error(response.data.msg);
                  }
                })
            // console.log(auditSearchData.value);
          }else {
            ElMessage.warning("还没有创建组织!");
          }
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
onMounted(() => {
  search();
})

//定义函数用于审核通过
const auditPass = (roleId) => {
  // console.log("roleId:"+roleId);
  status.value = 20;
  let data = qs.stringify({status:status.value,id:roleId})
  axios.post(BASE_URL+"/isms/role/updateStatus",data)
      .then((response) => {
        if(response.data.code == 200){
          ElMessage.success("审核成功!")
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
//定义函数用于审核不通过
const auditReject = (roleId) => {
  status.value = 30;
  let data = qs.stringify({status:status.value,id:roleId});
  axios.post(BASE_URL+"/isms/role/updateStatus",data)
      .then((response) => {
        if(response.data.code == 200){
          ElMessage.success("审核成功!")
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
//状态码格式转换
const roleTypeFormatters = (row,column,cellValue,index) => {
  if(cellValue==10){
    cellValue = "行政";
  }else if(cellValue==20){
    cellValue = "财务";
  }else if(cellValue==30){
    cellValue = "法务";
  }else if(cellValue==40){
    cellValue = "人事";
  }else if(cellValue==50){
    cellValue = "IT";
  }else if(cellValue==60){
    cellValue = "其它";
  }
  return cellValue;
}
</script>

<style scoped>

</style>